﻿using MediatR;
using S.PregnancyTest.API.Application.Command;
using S.PregnancyTest.Domain;

namespace S.PregnancyTest.API.Application.CommandHandlers
{
    public class UpdatePregnancyTestHandler : IRequestHandler<UpdatePregnancyTestCommand, bool>
    {

        private readonly MyDbContext dbContext;

        public UpdatePregnancyTestHandler(MyDbContext dbContext)
        {
            this.dbContext = dbContext;
        }

        public async Task<bool> Handle(UpdatePregnancyTestCommand request, CancellationToken cancellationToken)
        {
            //根据耳号获取到对应的数据
            var pregnancyTestModel = dbContext.PregnancyTestModel.Where(x => x.EarTag == request.EarTag).OrderByDescending(x => x.CreateDate).FirstOrDefault();

            pregnancyTestModel.SheepStatus = pregnancyTestModel.SheepStatus == true ? false : true; // 修改羊只状态

            //执行修改操作
            dbContext.PregnancyTestModel.Update(pregnancyTestModel);
            return dbContext.SaveChanges() > 0;
        }
    }
}
